class Train:
    def __init__(self, data=None):
        self.data = data
        self.next = None
        self.prev = None


class List():
    def __init__(self):
        self.head = Train()
        self.tail = Train()
        self.length = 0

    def insert(self, pos, data):
        assert pos >= 0 and pos <= self.length

        tmp = self.head
        while pos > 0:
            tmp = tmp.next
            pos -= 1

        newlist = Train()
        newlist.data = data
        # if tmp.next is None:
        #     tmp.next = newlist
        #     newlist.prev = tmp
        # else:
        #     newlist.next = tmp.next
        #     newlist.prev = tmp
        #     tmp.next = newlist
        #     tmp.next.prev = newlist
        # self.length += 1
        nowlist = tmp
        nextlist = tmp.next

        nowlist.next = newlist
        newlist.prev = nowlist

        newlist.next = nextlist
        if nextlist:
            nextlist.prev = newlist

        self.length += 1




#遍历
    def alllist(self, flag=True):
        #正序
        if flag:
            tmp = self.head.next
            while tmp is not None:
                yield tmp.data
                tmp = tmp.next
        #逆序
        else:
            tmp = self.tail.prev
            while tmp is not None:
                yield tmp.data
                tmp = tmp.prev

    def append(self, data):
        return self.insert(self.length, data)

    def remove(self, pos):
        assert pos> 0 and pos<= self.length

        tmp = self.head
        while pos > 0:
            tmp = tmp.next
            pos -= 1

        prevtmp = tmp.prev
        nexttmp = tmp.next

#注意条件前后，注释部分有问题
        # if tmp.next:
        #     prevtmp.next = tmp.next
        #     nexttmp.prev = tmp.prev
        prevtmp.next = tmp.next
        if tmp.next:
            nexttmp.prev = tmp.prev
        self.length -= 1
#魔法函数，可以根据索引操作， 迭代器
    def __iter__(self):
        print('访问iter')
        tmp = self.head.next
        while tmp != self.tail:
            return self.alllist()

    def __setitem__(self, key, value):
        print('访问setitem')
        assert isinstance(key, int)
        self.insert(key, value)

    def __delitem__(self, key):
        print('访问del')
        assert isinstance(key, int)
        self.remove(key)

    def __getitem__(self, key):
        print('访问get')
        assert isinstance(key, int)
        assert key >= 0 and key < self.length

        tmp = self.head.next
        while key > 0:
            tmp = tmp.next
            key -= 1
        return tmp.data

    def print_iter(x):
        for l in range(0, x.length):
            print(x[l])


#队列、是否空、是否满
    def unshift(self, data):
        tmp = self.tail
        newlist = Train()
        newlist.data = data

        prevlist = tmp.prev

        newlist.prev = prevlist
        newlist.next = self.tail

    def shift(self):
        pass


#next
if __name__ == '__main__':

    l= List()
    l.append(1)
    l.append(2)
    l.append(3)
    l.append(4)
    l.append(5)
    for x in l.alllist():
        print(x)

    print('=========insert==========')
    l.insert(3, 900)
    for x in l.alllist():
        print(l[1])

    print('==========remove==========')
    l.remove(5)
    for x in l:
        print(x)
    # print('==========unshift=========')
    # l.unshift(100)
    # for x in l:
    #     print(x)

    l.print_iter()

